package com.hongfeng.support.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.common.Constants;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.item.tag.LogicOperator;
import com.hongfeng.support.model.User;
import com.hongfeng.support.service.UserService;
import com.hongfeng.support.util.WebHelper;
import com.hongfeng.support.util.encrypt.DESUtil;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <登录>
 * 
 * @author llcyyc
 * @date 创建时间：2013-1-8 上午10:45:20
 */
@Controller
public class LoginController extends BaseController {

	@Resource
	private UserService userService;

	/**
	 * 
	 * <登录>
	 * 
	 * @param request
	 * @param response
	 * @param userInfo
	 * @throws Exception
	 *             void
	 * @see
	 * @throws
	 */
	@RequestMapping("login")
	public void login(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("loginInfo") String loginInfo) throws Exception {
		if (StringUtils.isEmpty(loginInfo)) {
			this.sendErrorResponse(response, "用户名或密码不能为空。");
			return;
		}
		JSONObject jsonObject = JsonUtils.json2Object(loginInfo);
		String username = (String) jsonObject.get("username");
		String password = (String) jsonObject.get("password");
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			this.sendErrorResponse(response, "用户名和密码不能为空。");
			return;
		}
		DESUtil des = new DESUtil();
		List<ParmItem> items = new ArrayList<ParmItem>(2);
		items.add(new ParmItem("username", username, FieldOperator.EQUAL,
				LogicOperator.AND));
		items.add(new ParmItem("password", des.encrypt(password),
				FieldOperator.EQUAL, LogicOperator.AND));
		List<User> list = userService.findAllByQuery(User.class, items);
		if (list != null && list.size() > 0) {
			User user = list.get(0);
			short isRoleEnable = user.getRole().getIsEnable().shortValue();
			short isUserEnable = user.getIsEnable().shortValue();
			if (isRoleEnable == 1 && isUserEnable == 1) {
				WebHelper.setSessionAttribute(request,
						Constants.SESSION_LOGIN_USER, user);
			} else if (isRoleEnable == 0) {
				this.sendErrorResponse(response, "角色无效");
			} else if (isUserEnable == 0) {
				this.sendErrorResponse(response, "账户无效");
			} else {
				this.sendErrorResponse(response, "登录失败");
			}
		} else {
			this.sendErrorResponse(response, "用户名或密码错误。");
		}
	}

	/**
	 * 
	 * <shifou>
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 *             void
	 * @see
	 * @throws
	 */
	@RequestMapping("logout")
	public void logout(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WebHelper.removeSessionAttribute(request, Constants.SESSION_LOGIN_USER);
	}

	/**
	 * 
	 * <检查是否登录>
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 *             void
	 * @see
	 * @throws
	 */
	@RequestMapping("isLogin")
	public void isLogin(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = WebHelper.getSessionAttribute(request,
				Constants.SESSION_LOGIN_USER);
		if (user != null) {
			this.sendSuccResponse(response, JsonUtils.bean2Json(user));
		} else {
			this.sendErrorResponse(response, "未登录");
		}
	}

}
